package org.example.config;

import org.example.interceptor.ApiSecurityInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description:
 *
 * @author shenguangyang
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * api安全拦截器
     * @return
     */
    @Bean
    public ApiSecurityInterceptor apiSecurityInterceptor() {
        return new ApiSecurityInterceptor();
    }

    /**
     * 不管哪个版本，addResourceHandler方法是设置访问路径前缀，
     * addResourceLocations方法设置资源路径，如果你想指定外部的目录也很简单，
     * 直接addResourceLocations指定即可，代码如下：
     * registry.addResourceHandler("/static/**").addResourceLocations("file:E:/cxy/");
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(apiSecurityInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/static/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
